import moTicketPanel from "../../../components/dialog-ticket-panel/panel.vue";

export default {
  name: "ConfirmOrder",
  components: {
    "mo-ticket-panel": moTicketPanel
  },
  filters: {
    numFilter(value) {
      // 截取当前数据到小数点后两位
      let realVal = parseFloat(value).toFixed(2);
      return realVal;
    }
  },
  data() {
    return {
      ids: "",
      sellerId: "",
      address: {},
      orderInfo: {},
      packetList: [],
      redPacketInfo: {},
      loading: true,
      memo: "",
      showBtn: true,
      docmHeight: document.documentElement.clientHeight //初始屏幕高度
    };
  },
  destroyed() {
    this.$callJava({ onBackPressed: false });
  },
  methods: {
    yasuo(img) {
      return img.indexOf("?") == -1 ? img + "?yasuo" : img;
    },
    goAddAddress() {
      //添加地址
      console.log(this.loading)
      if (!this.loading) {
        if (this.$fromApp(this)) {
          // this.$callJava({url:this.$webUrl+"address/add?from=app"})
          this.$router.push({ path: "/address/add", from: "app" });
        } else {
          this.$router.push({ path: "/address/add" });
        }
      }
    },
    goAddress() {
      //地址列表
      if (this.$fromApp(this)) {
        // this.$callJava({url:this.$webUrl+"cart/chooseAddress?from=app"})
        this.$router.push({
          path: "/cart/chooseAddress",
          query: {
            addressId: this.address.id,
            sellerId: this.sellerId,
            ids: this.ids,
            from: "app"
          }
        });
      } else {
        this.$router.push({
          path: "/cart/chooseAddress",
          query: {
            addressId: this.address.id,
            sellerId: this.sellerId,
            ids: this.ids
          }
        });
      }
    },
    initData() {
      //初始化确认订单信息
      if (!this.$storage.get("address")) {
        this.$storage.remove("address");
        this.$ajax.get(
          "address/get/default",
          res => {
            this.loading = false;
            if (res.code == 200) {
              this.address = res.data;
            }
          },
          {}
        );
      } else {
        this.loading = false;
      }

      this.$ajax.post(
        "replenish/sale_order/pre_order",
        { shoppingCartIds: this.ids, sellerId: this.sellerId },
        res => {
          // console.log("pre_order",res)
          if (res.code == 200) {
            this.orderInfo = res.data;
            this.redPacketInfo.id = res.data.redPacketId;
            this.redPacketInfo.value = res.data.redPacketAmount;

            this.$ajax.get(
              "replenish/red_packet/select",
              res => {
                // console.log("res111",res)
                if (res.code == 200) {
                  this.packetList = res.data;
                  if (this.packetList.length > 0) {
                    for (let i = 0; i < this.packetList.length; i++) {
                      if (this.packetList[i].id == this.redPacketInfo.id) {
                        this.packetList[i].checked = true;
                      }
                    }
                  }
                }
              },
              {
                pageNo: 1,
                pageSize: 1000,
                sellerId: this.sellerId,
                totalAmount: this.orderInfo.goodsAmount
              }
            );
          } else if (res.code == 20000) {
            this.$vux.toast.text("存在下架商品，请重新选择商品", "middle");
            setTimeout(() => {
              this.$router.go(-1);
            }, 2000);
          } else if (res.code == 20002) {
            this.$vux.toast.text(
              "存在赠品库存数量不足，请重新选择商品",
              "middle"
            );
            setTimeout(() => {
              this.$router.go(-1);
            }, 2000);
          } else {
            this.$vux.toast.text(res.msg, "middle");
            setTimeout(() => {
              this.$router.go(-1);
            }, 2000);
          }
        }
      );
    },
    commit() {
      if (this.address && this.address.id) {
        let _this = this
        this.$ajax.post(
          "replenish/sale_order/submit_order",
          {
            sellerId: this.sellerId,
            shoppingCartIds: this.ids,
            distributorAddressId: this.address.id,
            redPacketId: this.redPacketInfo.id ? this.redPacketInfo.id : null,
            memo: this.memo
          },
          res => {
            // console.log("submit order",res)

            if (res.code == 200) {
              if (this.$fromApp(this)) {
                if (res.data.payFlag) {
                  this.$callJava({ code: 1018, id: res.data.id })
                }
                this.$router.replace({ path: '/purchase/myPurchase', query: { id: this.$route.query.sellerId, from: 'app' } })
              } else {
                if (res.data.payFlag) {
                  this.$router.go(-1)
                  this.$ajax.post(
                    "payment/submit",
                    {
                      channel: "AllinPay",
                      fromType: "GoodsOrder",
                      allinPayMethod: "W02",
                      fromId: res.data.id,
                    },
                    (res) => {
                      // console.log(res)
                      if (res.code == 200) {
                        console.log(res.data);
                        window.location.href = res.data;
                        // this.$router.push(res.data);
                        // this.$router.push({ path:'/cart/pay/success'});
                      } else {
                        this.$vux.toast.text(res.msg, "middle");
                      }
                    }
                  );
                } else {
                  this.$router.replace({ path: '/purchase/myPurchase', query: { id: this.$route.query.sellerId} })
                }

              }
            } else {
              this.$vux.toast.text(res.msg, "middle");
            }
          }
        );
      } else {
        this.$vux.toast.text("请先添加地址后再进行提交！", "middle");
      }
    },
    chooseRedPacket() {
      // if(this.$fromApp(this)){
      //   this.$callJava({url:this.$webUrl+"cart/chooseRedPacket?from=app"})
      // }else{
      //   this.$router.push('/cart/chooseRedPacket')
      // }
      this.$refs.ticketNode.redPacketInit();
    },
    setPacket(packet) {
      // console.log('packet',packet)
      if (packet) {
        this.redPacketInfo = packet;
      } else {
        this.redPacketInfo = {};
      }
    },
    blurMemo() {
      // alert("11")
      this.$refs.ticketNode.isShow = true;
      setTimeout(() => {
        this.$refs.ticketNode.isShow = false;
      }, 100);
    }
  },
  mounted: function () {

    this.$callJava({ onBackPressed: true });
    // console.log("mounted")
    window["androidCallJs"] = this.androidCallJs;
    this.ids = this.$route.query.ids;
    this.sellerId = this.$route.query.sellerId;
    // console.log(this.$storage.get('address'))
    let that = this;
    if (this.$storage.get("address")) {
      // console.log("----")
      that.address = eval("(" + this.$storage.get("address") + ")");
      // console.log(that.address)
      // that.$storage.remove('address');
    }

    this.initData();

    // if(this.$route.query.address){
    //   this.address=eval('(' + this.$route.query.address + ')');
    // }

    window.onresize = () => {
      if (document.body.clientHeight < this.docmHeight) {
        this.showBtn = false;
        if (
          document.activeElement.tagName == "INPUT" ||
          document.activeElement.tagName == "TEXTAREA"
        ) {
          setTimeout(function () {
            document.activeElement.scrollIntoViewIfNeeded();
          }, 100);
        }
      } else {
        if (this.$refs && this.$refs.memoInput) {
          this.$refs.memoInput.blur();
        }
        this.showBtn = true;
      }
    };
  }
};
